using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DataPager
/// </summary>
public class DataPager
{
    DataTable _src;
    DataTable _des;
    int _curPage;
    int _recsPerPage;

    public int Records
    {
        get { return _src.Rows.Count; }
    }

    public int Pages
    {
        get
        {
            if (_src == null) return 0;
            return (this.Records / _recsPerPage) + ((this.Records % _recsPerPage == 0) ? 0 : 1);
        }
    }

    public string PagesString
    {
        get
        {
            return this.Pages.ToString();
        }
    }

    public int CurPage
    {
        get { return _curPage; }
    }

    public DataTable Table
    {
        get
        {
            Process();
            return _des;
        }
    }

    public DataPager()
    {
        _curPage = 0;
        _recsPerPage = Convert.ToInt32(ConfigurationManager.AppSettings["FloorsPerPage"]);
    }

    public DataPager(DataTable src, int curPage)
    {
        _src = src;
        _curPage = curPage;
        _recsPerPage = Convert.ToInt32(ConfigurationManager.AppSettings["FloorsPerPage"]);
    }

    private void Process()
    {
        if (_src == null) return;

        _des = new DataTable();

        //copy schema
        foreach (DataColumn col in _src.Columns)
        {
            _des.Columns.Add(col.ColumnName, col.DataType);
        }

        //calculate recodrs
        int begRec = _curPage * _recsPerPage;
        int recs = _src.Rows.Count;
        int tmp = recs - begRec;
        int nums = _recsPerPage < tmp ? _recsPerPage : tmp;
        
        //import records
        for (int i = begRec, n = begRec + nums; i < n; i++)
        {
            DataRow srcRow = _src.Rows[i];
            _des.ImportRow(srcRow);
        }
    }
}
